package cst.a305htr.recyclertest305htr.model;

import java.util.List;

import cst.a305htr.recyclertest305htr.bean.ArticleBean;
import cst.a305htr.recyclertest305htr.iface.ArticleIface;
import cst.a305htr.recyclertest305htr.iface.ArticleListen;
import cst.a305htr.recyclertest305htr.service.ArticleService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/16.
 */


public class ArticleModel implements ArticleIface {
    private Retrofit retrofit;
    private static String BASE_URL="http://amicool.neusoft.edu.cn/";
    public ArticleModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    @Override
    public void getResultList(String mod, int page, String SessionID,final ArticleListen listen) {
        ArticleService service=retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = service.getResult(mod,page,SessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listen.OnResponse(response.body());
                }
                else
                {
                    listen.OnFail("error");
                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listen.OnFail(t.toString());
            }
        });
    }
}

